using UnityEngine;
using System.Collections;

public class BackpackItem : MonoBehaviour
{
	public GameObject operationLayer;
	
	public enum StorageStateType
	{
		Backpack,					//背包//
		RoleInfo,					//任务面板//
		Warehouse, 					//仓库//
		Arrangement,				//布阵//
		Other,
	}
	
	public UISprite icon;
	public UISprite iconBG;
	public UILabel countLabel;
	public int index;
	public StorageStateType storageState = StorageStateType.Backpack;
	public Item myItem;
	public BackpackItem targetItem;
	bool longPress = false;
	public bool canDrop = false;
	public float pressDetection = 0.3f;
	public UISprite replaceSprite;
	bool isDragging = false;
	float pressTime;
	Camera uiCameraLayer;
	UICamera uiCamera;
	private GameObject currentHitObject;
	float currentTime;
	ClipAnimation clipAni;
	
	void Awake ()
	{
	}
	
	// Use this for initialization
	void Start ()
	{
		uiCameraLayer = Camera.main;
		
		if (uiCameraLayer == null)
			uiCameraLayer = NGUITools.FindCameraForLayer (gameObject.layer);
		
		uiCamera = uiCameraLayer.GetComponent<UICamera> ();
		
		if (currentHitObject == null)
			currentHitObject = gameObject;
		
		if (operationLayer == null) {
			Debug.Log ("layer is null");
		}
		
		//计算出该背包格子的index
		if (storageState == StorageStateType.Backpack || storageState == StorageStateType.Warehouse) {
			if (myItem == null) {
				myItem = new Item (icon.spriteName, GameDefine.ITEMLOCKED);
			} else {
				icon.spriteName = myItem.SpriteName;
				icon.MakePixelPerfect ();
			}
		} else if (storageState == StorageStateType.RoleInfo) {
			if (myItem == null) {
				myItem = new Item (icon.spriteName, false, false);
			} else {
				icon.spriteName = myItem.SpriteName;
				icon.MakePixelPerfect ();
			}
		} else if(storageState == StorageStateType.Arrangement){
			clipAni = GetComponentInChildren<ClipAnimation>();
			if (myItem == null) {
				myItem = new Item (clipAni.filePath);
				myItem.Visible = GameDefine.INVISIABLE;
			} else {
				clipAni.SetAtlas(myItem.ModelName);
				
				icon.spriteName = myItem.SpriteName;
				icon.MakePixelPerfect ();
			}
		} else {
			if (myItem == null) {
				myItem = new Item (icon.spriteName, true, false);
			} else {
				icon.spriteName = myItem.SpriteName;
				icon.MakePixelPerfect ();
			}
		}
		
		icon.enabled = myItem.Visible;
		
		if(iconBG != null)
			iconBG.enabled = myItem.Visible;
		
		if(replaceSprite != null)
			replaceSprite.enabled = myItem.Visible;
		
		if (countLabel != null) {
			if (myItem.ItemCount <= 1) {
				countLabel.enabled = GameDefine.INVISIABLE;
			} else {
				countLabel.enabled = GameDefine.VISIABLE;
			}
		}
	}
	
	// Update is called once per frame
	void Update ()
	{
		currentTime = Time.realtimeSinceStartup;
		if (currentTime > pressTime + pressDetection && longPress == true && Cursor.isDraggingPanel == false) {
			if (!Input.anyKey)
				return;
			
			Ray ray = uiCameraLayer.ScreenPointToRay (Input.mousePosition);
			RaycastHit hit;
			if (Physics.Raycast (ray, out hit)) {
				currentHitObject = hit.collider.gameObject;
				if (currentHitObject != gameObject) {
					return;
				}
			}
			
			if (myItem.Visible) {
				if (iconBG != null) {
					Cursor.Set (icon.atlas, icon.spriteName, iconBG.spriteName);
					iconBG.enabled = false;
				} else {
					Cursor.Set (icon.atlas, icon.spriteName, "");
				}
				
				icon.enabled = false;
				
				canDrop = true;
			}
		}
	}
	
	/*
	void OnClick ()
	{
		longPress = false;
		Debug.Log ("OnClick" + "<---->" + Time.realtimeSinceStartup);
	}
	*/
	
	void OnPress (bool press)
	{
		//OnPress是成对响应的,一次点击操作响应两次//
		longPress = press;
		
		if (longPress == false) {
			if (currentHitObject == gameObject) {
				Cursor.Clear ();
				if (myItem.Visible) {
					icon.enabled = true;
					if (iconBG != null)
						iconBG.enabled = true;
				}
			}
			
			if (currentTime <= pressTime + pressDetection && Cursor.isDraggingPanel == false) {
				if (myItem.Locked == GameDefine.ITEMLOCKED) {
					//开启背包格子//
					canDrop = true;
					
					operationLayer.SendMessage ("UnLockItemRequest", this, SendMessageOptions.DontRequireReceiver);
				} else {
					//弹tips//
					operationLayer.SendMessage ("PopTipsWindowRequest", this, SendMessageOptions.DontRequireReceiver);
				}
			}
			
			Cursor.isDraggingPanel = false;
		} else {
			
		}
		
		pressTime = Time.realtimeSinceStartup;
		
		if (canDrop == false)
			return;
		
		Drop (currentHitObject);
	}
	
	void Drop (GameObject go)
	{
		if (go != null && go != gameObject) {
			BackpackItem item = go.GetComponent<BackpackItem> ();
			
			if (item == null) {
				if (go.name.Equals ("Background")) {
					//删除物品TODO//
					operationLayer.SendMessage ("DeleteItemRequest", this, SendMessageOptions.DontRequireReceiver);
					Cursor.Clear ();
					icon.enabled = GameDefine.INVISIABLE;
					if (iconBG != null)
						iconBG.enabled = GameDefine.INVISIABLE;
				} else {
					Cursor.Clear ();
					icon.enabled = myItem.Visible;
					if (iconBG != null)
						iconBG.enabled = myItem.Visible;
				}
			} else {
				//移动物品或者换装//
				targetItem = item;
				operationLayer.SendMessage ("MoveOrEquipRequest", this, SendMessageOptions.DontRequireReceiver);
			}
		}
	}
	
	void OnDrag (Vector2 delta)
	{
		
	}
	
	public void Refresh ()
	{
		icon.spriteName = myItem.SpriteName;
		icon.enabled = myItem.Visible;
		icon.MakePixelPerfect ();
		
		if (iconBG != null) {
			iconBG.spriteName = myItem.BgSpriteName;
			iconBG.enabled = myItem.Visible;
			iconBG.MakePixelPerfect ();
		}
		
		if (countLabel != null) {
			countLabel.text = myItem.ItemCount.ToString ();
			if (myItem.ItemCount <= 1) {
				countLabel.enabled = false;
			} else {
				countLabel.enabled = true;
			}
		}
		
		if(clipAni != null){
			clipAni.SetAtlas(myItem.ModelName);
		}
		
	}
}
